"""
Case Type   : 一线问题
Case Name   : 兼容B库下，创建range分区表，指定range分区键为组合主键约束且含大写，验证gs_dump导出导入
Create At   : 2025/06
Owner       : haomeng
Description :
    1、创建两个B库
    2、连接B库1，创建range分区表，指定range分区键为组合主键约束且含大写，插入数据
    3、gs_dump导出B库中的数据，导出格式为文本格式
    4、将导出的数据导入到B库2中
    5、连接B库2，查询range分区表数据
    6、清理环境，恢复环境初始状态
Expect      :
    1、创建B库成功
    2、创建表成功，插入数据成功
    3、导出数据成功
    4、导入数据成功
    5、数据导入成功，查询数据成功
    6、清理环境成功
History     :
"""

import os
import time
import unittest

from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from yat.test import Node
from yat.test import macro


class DisasterRTOStandbyRead(unittest.TestCase):

    def setUp(self):
        self.log = Logger()
        self.log.info(f'----{os.path.basename(__file__)} start----')
        self.pri_sh = CommonSH('PrimaryDbUser')
        self.usernode = Node('PrimaryDbUser')
        self.constant = Constant()

        self.db_name = ['b_db1', 'b_db2']
        self.table = 't_range_partition_0002'
        self.dump_path = os.path.join(macro.DB_BACKUP_PATH, f'{self.table}.sql')

    def test_rto_standby_read(self):
        text = '-----step1:创建两个B库;  expect:创建成功-----'
        self.log.info(text)

        for db in self.db_name:
            value = self.pri_sh.execut_db_sql(f"create database {db} with dbcompatibility='B';")
            self.log.info(value)
            self.assertIn(self.constant.CREATE_DATABASE_SUCCESS, value, '创建B库失败')

        text = '-----step2:连接B库1，创建range分区表，指定range分区键为大写，插入数据  ' \
               'expect:创建成功，插入数据成功-----'
        self.log.info(text)

        sql1 = f"drop table if exists {self.table} cascade;" \
               f"create table {self.table}(id int,name varchar(200),age int,tel int,primary key(id,age))" \
               f"partition by range(ID)" \
               f"(partition p1 values less than(5000), partition p2 values less than(10000)," \
               f" partition p3 values less than(15000), partition p4 values less than(20000)," \
               f" partition p5 values less than(25000), partition p6 values less than(30000)," \
               f" partition p7 values less than(40000), partition p8 values less than(50000));" \
               f" declare" \
               f"     i integer;" \
               f" begin" \
               f"    for i in 1..10 loop" \
               f"    insert into {self.table} values(i, 'name_'||i, random()*100, random()*10000);" \
               f"end loop;" \
               f"end;"
        self.log.info(sql1)

        res1 = self.pri_sh.execut_db_sql(sql1, dbname=self.db_name[0])
        self.log.info(res1)
        self.assertIn(self.constant.CREATE_TABLE_SUCCESS, res1, '建表失败')
        self.assertIn(self.constant.CREATE_ANONYMOUS_BLOCK_SUCCESS_MSG, res1, '匿名块执行失败，插入数据失败')

        text = '-----step3:gs_dump导出B库中的数据，导出格式为文本格式  expect:导出成功-----'
        self.log.info(text)

        dump = f"source {macro.DB_ENV_PATH};gs_dump -p {self.usernode.db_port} {self.db_name[0]} -f {self.dump_path} -F p"
        self.log.info(dump)
        dump_res = self.usernode.sh(dump).result()
        self.log.info(dump_res)
        self.assertIn(self.constant.GS_DUMP_SUCCESS_MSG, dump_res, '导出失败')

        text = '-----step4:将导出的数据导入到B库2中  expect:导入成功-----'
        self.log.info(text)

        restore = f"source {macro.DB_ENV_PATH};gsql -d {self.db_name[1]} -f {self.dump_path}"
        self.log.info(restore)
        restore_res = self.usernode.sh(restore).result()
        self.log.info(restore_res)
        self.assertNotIn('ERROR', restore_res, '导入失败')

        text = '-----step5:连接B库2，查询range分区表数据  expect:查看成功，与原数据一致-----'
        self.log.info(text)

        select = f'select * from {self.table};select count(*) from {self.table};'
        self.log.info(select)
        select_res = self.pri_sh.execut_db_sql(select, dbname=self.db_name[1])
        self.log.info(select_res)
        self.assertIn('10 rows', select_res, '查询结果与原数据不一致')

    def tearDown(self):
        text = '-----step6:清理环境，恢复环境初始状态  expect:清理成功，恢复环境成功-----'
        self.log.info(text)

        rm_dump = f"rm -rf {self.dump_path};ls {self.dump_path};"
        self.log.info(rm_dump)
        rm_dump_res = self.usernode.sh(rm_dump).result()
        self.log.info(rm_dump_res)

        for db in self.db_name:
            drop_res = self.pri_sh.execut_db_sql(f'drop database {db};')
            self.log.info(drop_res)
            self.assertIn(self.constant.DROP_DATABASE_SUCCESS, drop_res, '清理环境失败')

        self.assertIn('No such file or directory', rm_dump_res, '清理环境失败')
        self.log.info(f'----{os.path.basename(__file__)} end----')
